<?php


namespace app\common\model;
use think\Model;
use think\Db;

class Car extends Model
{
    protected $table = "store_car";

    public function add($param){
        $find = self::where(['car_number'=>$param['car_number']])->find();
        if(empty($find)){
            $param['create_at'] = date('Y-m-d H:i:s');
            self::insert($param);
            return 1;
        }else{
            return '车牌号重复,请勿重复提交！';
        }
    }

    public function index($param){
        return self::where(['user_id'=>$param['user_id']])->where("delete_time is null")->select();
    }

    public function car_save($param){
        $user_id = $param['user_id'];
        $find = self::where(['car_number'=>$param['car_number']])->where("user_id!=$user_id")->find();
        if(empty($find)){
            $row = self::where(["id"=>$param['id']])->find();
            if($row){
                if($row['user_id']==$param['user_id']){
                    $param['create_at'] = date('Y-m-d H:i:s');
                    self::where(["id"=>$param['id']])->update($param);
                    return 1;
                }else{
                    return '这不是您的车辆,请勿提交！';
                }
            }else{
                return '车辆未找到！';
            }
        }else{
            return '车牌号重复,请勿重复提交！';
        }
    }


    public function del($param){
        $find = self::where(['id'=>$param['id']])->where(["user_id"=>$param['user_id']])->find();
        if(empty($find)){
            return '车辆未找到！';
        }else{
            self::where(['id'=>$param['id']])->where(["user_id"=>$param['user_id']])->update(['delete_time'=>date('Y-m-d H:i:s')]);
            return 1;
        }
    }

}